import { initSearchConfig, TableSearchPanel, XxpageContainer, XxRangePicker, XxTable } from "@/ui";
import React, { useEffect, useRef, useState } from "react";
import searchFields from "./config/searchFields";
import getColumns from "./config/columns";
import DictForm from "./dictForm";
import getActoinPanel from "./config/action";
import { globalModal, mobxState } from "@/data";
import { dictApi } from "@/api";
import { deleteConfirm, successAlert } from "@/common/methods";
import { useNavigate } from "react-router-dom";


/**@description 字典配置页面 */
const DictView = () => {
    const navigate = useNavigate();
    const dictRef = useRef();
    const [params, setParams] = useState({ ...initSearchConfig(searchFields) })
    const [state, setState] = useState({
        data: [],
        totalCount: 0
    })
    const [loading, setLoading] = useState(false);

    /**@description 跳转数据页面 */
    const gotoDetail = (dictType) => {

        navigate("/development/dict/detail?dictType=" + dictType, { state: { dictType } });
    }
    //搜索事件
    const onClickSearch = (value) => {
        setParams({
            ...params,
            ...value,
            current: 1,
        });
    };


    /**@description 查找字典列表 */
    const getList = async () => {
        setLoading(true);
        const { data, code, msg } = await dictApi.postDictList({ ...params });
        setState({
            data: [...data.list],
            totalCount: data.totalCount || 0
        })
        setLoading(false);
    }

    const onChange = (current, pageSize) => {
        setParams({
            ...params,
            current,
            pageSize
        })
    }



    /**  @description 打开编辑窗口 */
    const open = ({ type = String, record = Object }) => {
        dictRef.current.open({
            type,
            record
        });
    }

    /**@description 删除按钮操作 */
    const handleDelete = async (dictNo) => {
        try {
            await deleteConfirm();
            const { code, msg } = await dictApi.deleteDict(dictNo)
        } catch (error) {

        } finally {
            getList()
        }
    }





    const [selectedKeys, setSelectedKeys] = useState([])
    const onSelectChange = (newSelectedRowKeys, selectedRows) => {
        setSelectedKeys(newSelectedRowKeys);
    }
    const columns = getColumns({
        open, handleDelete, gotoDetail, getList
    })
    const actions = getActoinPanel({ open: open })
    const tableProps = {
        tablePanelConfig: actions,
        loading: loading,
        columns: columns,
        dataSource: state.data,
        pagination: {
            total: state.totalCount,
            pageSize: params.pageSize,
            onChange: onChange,
            current: params.current
        },
        rowSelection: {
            type: "checkbox",
            selectedRowKeys: selectedKeys,
            onChange: onSelectChange,
        },
        rowKey: 'dictNo',
        selectedRowKeys: selectedKeys,
        onRow: (record, index) => {
            return {
                onDoubleClick: () => {
                    console.log("双击", record);
                }
            }
        },
    }


    useEffect(() => {
        getList()
    }, [params])
    return (
        <XxpageContainer>
            <DictForm onRef={dictRef} getList={getList} />
            <TableSearchPanel
                fields={searchFields}
                onSearch={onClickSearch}

            />
            <XxTable  {...tableProps} />

        </XxpageContainer>
    )
}

export default DictView;